<?php
/**
 * Controller is the customized base controller class.
 * All controller classes for this application should extend from this base class.
 */
class Controller extends CController
{
	/**
	 * @var string the default layout for the controller view. Defaults to '//layouts/column1',
	 * meaning using a single column layout. See 'protected/views/layouts/column1.php'.
	 */
	public $layout='//layouts/column1';
	/**
	 * @var array context menu items. This property will be assigned to {@link CMenu::items}.
	 */
	public $menu=array();
	public $viewData = array();
	/**
	 * @var array the breadcrumbs of the current page. The value of this property will
	 * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}
	 * for more details on how to specify this property.
	 */
	public $breadcrumbs=array();
	
	public $is_admin=false;
	public function init()
	{
		
	}
	protected function beforeRender($view)
	{	
		if(Yii::app()->theme == null){
			//$m = new CThemeManager;
			//Yii::app()->theme = $m->getTheme("bootstrap");
		}
		$this->combineScript();
		$this->composeNavBar();
		return true;
	}
	protected function beforeAction($action)
	{	
		$pathInfo = Yii::app()->request->getPathInfo();
		if(strpos($pathInfo, 'admin')=== 0)
		{
			$this->ifAdminEnd();
		}
		else
		{
			$this->ifDesktopFrontEnd();
		}
		return true;
	}
	private function ifAdminEnd()
	{
		$this->is_admin = true;
		$this->layout='//layouts/column1';
	}
	private function ifDesktopFrontEnd()
	{
		
	}
	private function combineScript()
	{
		if(Yii::app()->request->isAjaxRequest)
		{
			$this->layout='//layouts/column3';
			Yii::app()->clientscript->scriptMap['jquery.js'] = false;
		}
		else
		{
			//echo Yii::app()->basePath;
			//echo Yii::app()->theme->basePath;exit;
			//Yii::app()->bootstrap->register();
			$cs = Yii::app()->getClientScript();
			$themePath = Yii::app()->theme->basePath;
			$less = $themePath.'/assets/bootstrap-3.0.3/less/bootstrap.less';
			$less = realpath($less);
			$bootstrap_dest = $themePath.'/assets/bootstrap-3.0.3/dist/css/bootstrap.css';
			$bootstrap_dest = realpath($bootstrap_dest);
			//if(file_exists($less) && file_exists($bootstrap_dest) && filemtime($less) > filemtime($bootstrap_dest)){
				$is_compiled = Yii::app()->less->compileFile($less,$bootstrap_dest,false);
			//}
			
			$less = $themePath.'/assets/bootstrap-3.0.3/less/theme.less';
			$less = realpath($less);
			$bootstrap_dest = $themePath.'/assets/bootstrap-3.0.3/dist/css/bootstrap-theme.css';
			$bootstrap_dest = realpath($bootstrap_dest);//exit;
			//if(file_exists($less) && file_exists($bootstrap_dest) && filemtime($less) > filemtime($bootstrap_dest)){
				$is_compiled = Yii::app()->less->compileFile($less,$bootstrap_dest,false);
			//}
			$themeUrl = Yii::app()->theme->baseUrl;
			$cs->registerCssFile($themeUrl.'/assets/bootstrap-3.0.3/dist/css/bootstrap.css')
				->registerCssFile($themeUrl.'/assets/bootstrap-3.0.3/dist/css/bootstrap-theme.css');
	
	
			$cs->registerScriptFile($themeUrl.'/assets/bootstrap-3.0.3/dist/js/bootstrap.min.js')
				->registerScript('tooltip',
					"$('[data-toggle=\"tooltip\"]').tooltip();
					$('[data-toggle=\"popover\"]').tooltip()"
					,CClientScript::POS_READY);
			
			$cs->registerScriptFile(
				Yii::app()->baseUrl.'/js/underscore.js'
			);
			$cs->registerScriptFile(
				Yii::app()->baseUrl.'/js/backbone.js'
			);
			$cs->registerScriptFile(
				Yii::app()->baseUrl.'/js/jquery.form.min.js'
			);
			$cs->registerScriptFile(
				Yii::app()->baseUrl.'/js/jquery.blockUI.js'
			);
			$cs->registerScriptFile(
				Yii::app()->baseUrl.'/js/common.js'
			);
			//Yii::app()->bootstrap->register();
			$cs->registerScript('cfg', "var CFG = ".json_encode(array('baseUrl' => Yii::app()->baseUrl)),CClientScript::POS_HEAD);

		}
	}
	private function composeNavBar(){
		$this->viewData['userMenuItems'] = array(
			'class' => 'bootstrap.widgets.BsNav',
            //'htmlOptions'=>array('class'=>'pull-right'),
			'htmlOptions' => array(
                'pull' => BSHtml::NAVBAR_NAV_PULL_RIGHT
            ),
			'id' => 'js-right-nav',
			'type' => 'navbar',
            'activateParents' => true,
            'items'=>array(
                array('htmlOptions' => array('id' => 'js-login-link'),'label'=>'Login', 'url'=>array('/user/login'), 'visible'=>Yii::app()->user->isGuest),
                array('htmlOptions' => array('id' => 'js-logout-link'),'label'=>'Logout ('.Yii::app()->user->name.')', 'url'=>'#', 'visible'=>!Yii::app()->user->isGuest, 'items'=>array(
                    array('label'=>'Profile', 'url'=>array('/user/profile')),
					array('label'=>'Edit Profile', 'url'=>array('/user/profile/edit')),
    				array('label'=>'Change password', 'url'=>array('/user/profile/changepassword')),
                    array('label'=>'Logout', 'url'=> array('/site/logout')),
                )),
            ),
        );
		$this->viewData['nav'] = $this->widget('bootstrap.widgets.BsNavbar', array(
		    'collapse' => true,
		    'brandLabel' => BSHtml::icon(BSHtml::GLYPHICON_HOME),
		    'brandUrl' => Yii::app()->homeUrl,
			'htmlOptions' => array('id' => 'js-nav','class' => 'navbar navbar-fixed-top'),
			'position' => BSHtml::NAVBAR_POSITION_FIXED_TOP,
			'brandLabel' => 'Project Name',
		    'items' => array(
		        array(
		            'class' => 'bootstrap.widgets.BsNav',
		            'type' => 'navbar',
		            'activateParents' => true,
		            'items' => array(
		                array(
		                    'label' => 'Home',
		                    'url' => array(
		                        '/site/index'
		                    ),
		                    //'items' => array()
		                ),
						array(
		                    'label' => 'About',
		                    'url' => array(
		                        '/site/page',
		                        'view' => 'about'
		                    )
		                ),
		                array(
		                    'label' => 'Contact',
		                    'url' => array(
		                        '/site/contact'
		                    )
		                )
		            )
		        ),
				$this->viewData['userMenuItems']
		    )
		),true);
	}
	public function actionError()
	{
	    print_r(Yii::app()->errorHandler->error);exit;
		if($error=Yii::app()->errorHandler->error)
	        $this->render('error', $error);
	}
}